package com.zhengshang.quartz.task;

import com.zhengshang.api.domain.AppContributionRecord;
import com.zhengshang.api.mapper.AppContributionRecordMapper;
import com.zhengshang.common.core.domain.entity.SysUser;
import com.zhengshang.common.utils.DateUtils;
import com.zhengshang.system.mapper.SysUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("partnerTask")
public class PartnerTask
{

    protected final Logger logger = LoggerFactory.getLogger(PartnerTask.class);
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private AppContributionRecordMapper appContributionRecordMapper;

    public void dataRepair()
    {
        String[] phones = {"13782849994", "15239000502", "18567075985"};
        Map<String, SysUser> userMap = new HashMap<>();
        Map<String, BigDecimal> userValueMap = new HashMap<>();
        for (String phone : phones) {
            SysUser sysUser = userMapper.selectUserByUserName(phone);
            userMap.put(phone, sysUser);
            userValueMap.put(phone, new BigDecimal(0));
        }

        BigDecimal memberValue = new BigDecimal(0.1);
        BigDecimal vipValue = new BigDecimal(1);
        SysUser userQuery = new SysUser();
        userQuery.setUserType("01");
        List<SysUser> allUserList = userMapper.queryAllAppUser(userQuery);
        for (SysUser sysUser : allUserList) {
            if (sysUser.getUpUserId() == null) {
                continue;
            }
            SysUser partnerUser = getPartnerUser(sysUser);
            if (partnerUser != null && userMap.get(partnerUser.getPhonenumber()) != null) {
                sysUser.setPertainToPartnerId(partnerUser.getUserId());
                userMapper.bandPartnerId(sysUser);

                if ("2".equals(sysUser.getAppMemberType()) || "3".equals(sysUser.getAppMemberType())) {
//                    partnerUser.setContributionValue(partnerUser.getContributionValue().add("2".equals(sysUser.getAppMemberType()) ? memberValue : vipValue));
//                    userMapper.plusContributionValue(partnerUser);
                    BigDecimal value = "2".equals(sysUser.getAppMemberType()) ? memberValue : vipValue;
                    userValueMap.put(partnerUser.getPhonenumber(), userValueMap.get(partnerUser.getPhonenumber()).add(value));
                    AppContributionRecord appContributionRecord = new AppContributionRecord();
                    appContributionRecord.setUserId(partnerUser.getUserId());
                    appContributionRecord.setFromUserId(sysUser.getUserId());
                    appContributionRecord.setContributionNum(value);
                    appContributionRecord.setCreateBy(sysUser.getUserName());
                    appContributionRecord.setCreateTime(DateUtils.getNowDate());
                    appContributionRecordMapper.insertAppContributionRecord(appContributionRecord);
                }
            }
        }

        for (String key : userValueMap.keySet()) {
            SysUser user = userMap.get(key);
            user.setContributionValue(userValueMap.get(key));
            userMapper.plusContributionValue(user);
        }
        logger.info("贡献值更新完成！");
    }

    private SysUser getPartnerUser(SysUser sysUser){
        if (sysUser.getUpUserId() == null) {
            return null;
        }
        if ("1".equals(sysUser.getIsPartner())) {
            return null;
        }
        SysUser upSysUser = userMapper.selectUserById(sysUser.getUpUserId());
        if ("1".equals(upSysUser.getIsPartner())) {
            return upSysUser;
        }
        if (upSysUser.getPertainToPartnerId() != null) {
            return userMapper.selectUserById(upSysUser.getPertainToPartnerId());
        }
        return getPartnerUser(upSysUser);
    }
}
